package com.example.finalproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class AddCollect extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private static  final String TAG = "right";
    ArrayList<Database> collect_list;
    ListView collectlist;
    MyCollectAdapter myCollectAdapter;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_collect);
        collect_list = new ArrayList<Database>();
        collectlist = findViewById(R.id.collect_list);
        MyCollectOpenHelper helper = new MyCollectOpenHelper(this,"collect_list.db",null,1);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from collect_list",null);
        while(cursor.moveToNext()){
            int index0 = cursor.getColumnIndex("name");
            int index1 = cursor.getColumnIndex("material");
            int index2 = cursor.getColumnIndex("step");
            String name = cursor.getString(index0);
            Log.i(TAG, "name: "+ name);
            String material = cursor.getString(index1);
            String step = cursor.getString(index2);
            Database list = new Database(name,material,step);
            collect_list.add(list);
        }
        Log.i(TAG, "collect_list: "+ collect_list);
        myCollectAdapter = new MyCollectAdapter(this,R.layout.list_item,collect_list);
        collectlist.setAdapter(myCollectAdapter);
        collectlist.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Database list1 = collect_list.get(position);
        String name = list1.getName();
        String material = list1.getMaterial();
        String step = list1.getStep();
        intent = new Intent(this,LcDetail.class);
        intent.putExtra("name_key",name);
        intent.putExtra("material_key",material);
        intent.putExtra("step_key",step);
        startActivity(intent);
    }
}